home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wdj0697.zip / SHAPPIR.ZIP / FASTSTR.H
C/C++ Source or Header  |  1997-03-31  |  9KB  |  328 lines

  1. /*
  2.     FastStr.h
  3.  
  4.     Defines class CFastString --
  5.     FAST replacement for MFC CString.
  6.  
  7.     By Dan Shappir, 1997.
  8. */
  9. #ifndef _FASTSTR_H
  10. #define _FASTSTR_H
  11.  
  12. class CFastString : public CString {
  13. public:
  14.     CFastString()                                                  {}
  15.     CFastString(const CFastString& stringSrc) : CString(stringSrc) {}
  16.     CFastString(const CString& stringSrc) : CString(stringSrc)     {}
  17.     CFastString(TCHAR ch, int nRepeat = 1) : CString(ch, nRepeat)  {}
  18.     CFastString(LPCSTR lpsz) : CString(lpsz)                       {}
  19.     CFastString(LPCWSTR lpsz) : CString(lpsz)                      {}
  20.     CFastString(LPCTSTR lpsz, int nLength)
  21.         : CString(lpsz, nLength)                                   {}
  22.     CFastString(const unsigned char* psz) : CString(psz)           {}
  23.     CFastString(const class CStringSumBase&);
  24.  
  25.     const CFastString& operator=(const CFastString& stringSrc)
  26.     {
  27.         CString::operator=(stringSrc);
  28.         return *this;
  29.     }
  30.     const CFastString& operator=(const CString& stringSrc)
  31.     {
  32.         CString::operator=(stringSrc);
  33.         return *this;
  34.     }
  35.     const CFastString& operator=(TCHAR ch)
  36.     {
  37.         CString::operator=(ch);
  38.         return *this;
  39.     }
  40. #ifdef _UNICODE
  41.     const CFastString& operator=(char ch)
  42.     {
  43.         CString::operator=(ch);
  44.         return *this;
  45.     }
  46. #endif
  47.     const CFastString& operator=(LPCSTR lpsz)
  48.     {
  49.         CString::operator=(lpsz);
  50.         return *this;
  51.     }
  52.     const CFastString& operator=(LPCWSTR lpsz)
  53.     {
  54.         CString::operator=(lpsz);
  55.         return *this;
  56.     }
  57.     const CFastString& operator=(const unsigned char* psz)
  58.     {
  59.         CString::operator=(psz);
  60.         return *this;
  61.     }
  62.     const CFastString& operator=(const CStringSumBase& sum)
  63.     {
  64.         return *this = CFastString(sum);
  65.     }
  66.  
  67.     const CFastString& operator+=(const CFastString& string)
  68.     {
  69.         CString::operator+=(string);
  70.         return *this;
  71.     }
  72.     const CFastString& operator+=(const CString& string)
  73.     {
  74.         CString::operator+=(string);
  75.         return *this;
  76.     }
  77.     const CFastString& operator+=(TCHAR ch)
  78.     {
  79.         CString::operator+=(ch);
  80.         return *this;
  81.     }
  82. #ifdef _UNICODE
  83.     const CFastString& operator+=(char ch)
  84.     {
  85.         CString::operator+=(ch);
  86.         return *this;
  87.     }
  88. #endif
  89.     const CFastString& operator+=(LPCTSTR lpsz)
  90.     {
  91.         CString::operator+=(lpsz);
  92.         return *this;
  93.     }
  94.     const CFastString& operator+=(const CStringSumBase& sum);
  95.  
  96.     LPTSTR Put(LPTSTR dst) const
  97.     {
  98.         int length = GetLength();
  99.         return static_cast<LPTSTR>(memcpy(dst, LPCTSTR(*this),
  100.                                     length*sizeof(TCHAR)))+length;
  101.     }
  102. };
  103.  
  104. class CStringSumBase {
  105.     CFastString    m_string;
  106.     CFastString Eval() const
  107.     {
  108.         return const_cast<CStringSumBase*>(this)->m_string = *this;
  109.     }
  110.     
  111. public:
  112.     virtual int GetLength() const       = 0;
  113.     virtual LPTSTR Put(LPTSTR) const   = 0;
  114.     BOOL IsEmpty() const               { return GetLength() == 0; }
  115.     
  116.     TCHAR GetAt(int nIndex) const    { return Eval().GetAt(nIndex); }
  117.     TCHAR operator[](int nIndex) const
  118.         { return Eval().GetAt(nIndex); }
  119.     operator LPCTSTR() const         { return Eval(); }
  120.     
  121.     int Compare(LPCTSTR lpsz) const  { return Eval().Compare(lpsz); }
  122.     int CompareNoCase(LPCTSTR lpsz) const
  123.     {
  124.         return Eval().CompareNoCase(lpsz);
  125.     }
  126.     int Collate(LPCTSTR lpsz) const  { return Eval().Collate(lpsz); }
  127.     
  128.     CFastString Mid(int nFirst, int nCount) const
  129.     {
  130.         return Eval().Mid(nFirst, nCount);
  131.     }
  132.     CString Mid(int nFirst) const    { return Eval().Mid(nFirst); }
  133.     CString Left(int nCount) const   { return Eval().Left(nCount); }
  134.     CString Right(int nCount) const  { return Eval().Right(nCount); }
  135.  
  136.     CString SpanIncluding(LPCTSTR lpszCharSet) const
  137.     {
  138.         return Eval().SpanIncluding(lpszCharSet);
  139.     }
  140.     CString SpanExcluding(LPCTSTR lpszCharSet) const
  141.     {
  142.         return Eval().SpanExcluding(lpszCharSet);
  143.     }
  144. };
  145.  
  146. template<class A,class B>
  147. class CStringSum : public CStringSumBase {
  148. protected:
  149.     const A&    m_a;
  150.     const B&    m_b;
  151. public:
  152.     CStringSum(const A& a, const B& b) : m_a(a), m_b(b)  {}
  153.     int GetLength() const
  154.     {
  155.         return m_a.GetLength()+m_b.GetLength();
  156.     }
  157.     LPTSTR Put(LPTSTR dst) const
  158.     {
  159.         return m_b.Put(m_a.Put(dst));
  160.     }
  161. };
  162.  
  163. template<class A>
  164. class CStringSumTCHAR1 : public CStringSumBase {
  165. protected:
  166.     const A&    m_a;
  167.     const TCHAR    m_b;
  168. public:
  169.     CStringSumTCHAR1(const A& a, const TCHAR b) : m_a(a), m_b(b) {}
  170.     int GetLength() const
  171.     {
  172.         return m_a.GetLength()+1;
  173.     }
  174.     LPTSTR Put(LPTSTR dst) const
  175.     {
  176.         *(dst = m_a.Put(dst)) = m_b;
  177.         return dst+1;
  178.     }
  179. };
  180.  
  181. template<class B>
  182. class CStringSumTCHAR2 : public CStringSumTCHAR1<B> {
  183. public:
  184.     CStringSumTCHAR2(const TCHAR a, const B& b)
  185.         : CStringSumTCHAR1<B>(b, a)  {}
  186.     LPTSTR Put(LPTSTR dst) const
  187.     {
  188.         *dst = m_b;
  189.         return m_a.Put(dst+1);
  190.     }
  191. };
  192.  
  193. template<class A>
  194. class CStringSumLPCTSTR1 : public CStringSum<A,TCHAR> {
  195. protected:
  196.     int m_length;
  197. public:
  198.     CStringSumLPCTSTR1(const A& a, const LPCTSTR b)
  199.         : CStringSum<A,TCHAR>(a, *b)  {}
  200.     int GetLength() const
  201.     {
  202.         const_cast<CStringSumLPCTSTR1*>(this)->m_length =
  203.             lstrlen(&m_b);
  204.         return m_a.GetLength()+m_length;
  205.     }
  206.     LPTSTR Put(LPTSTR dst) const
  207.     {
  208.         return static_cast<LPTSTR>(memcpy(m_a.Put(dst),
  209.                         &m_b, m_length*sizeof(TCHAR)))+m_length;
  210.     }
  211. };
  212.  
  213. template<class B>
  214. class CStringSumLPCTSTR2 : public CStringSumLPCTSTR1<B> {
  215. public:
  216.     CStringSumLPCTSTR2(const LPCTSTR a, const B& b)
  217.         : CStringSumLPCTSTR1<B>(b, a)  {}
  218.     LPTSTR Put(LPTSTR dst) const
  219.     {
  220.         memcpy(dst, &m_b, m_length*sizeof(TCHAR));
  221.         return m_a.Put(dst+m_length);
  222.     }
  223. };
  224.  
  225. inline CFastString::CFastString(const CStringSumBase& sum)
  226. {
  227.     AllocBuffer(sum.GetLength());
  228.     if ( GetLength() )
  229.         sum.Put(m_pchData);
  230. }
  231.  
  232. inline const CStringSum<CFastString,CFastString>
  233.    operator+(const CFastString& a, const CFastString& b)
  234. {
  235.     return CStringSum<CFastString,CFastString>(a, b);
  236. }
  237.  
  238. inline const CStringSum<CFastString,CFastString>
  239.    operator+(const CFastString& a, const CString& b)
  240. {
  241.     return a+static_cast<const CFastString&>(b);
  242. }
  243. inline const CStringSum<CFastString,CFastString>
  244.    operator+(const CString& a, const CFastString& b)
  245. {
  246.     return static_cast<const CFastString&>(a)+b;
  247. }
  248.  
  249. inline const CStringSumLPCTSTR1<CFastString>
  250.    operator+(const CFastString& a, const LPCTSTR b)
  251. {
  252.     return CStringSumLPCTSTR1<CFastString>(a, b);
  253. }
  254. inline const CStringSumLPCTSTR2<CFastString>
  255.    operator+(const LPCTSTR a, const CFastString& b)
  256. {
  257.     return CStringSumLPCTSTR2<CFastString>(a, b);
  258. }
  259.  
  260. inline const CStringSumTCHAR1<CFastString>
  261.    operator+(const CFastString& a, const TCHAR& b)
  262. {
  263.     return CStringSumTCHAR1<CFastString>(a, b);
  264. }
  265. inline const CStringSumTCHAR2<CFastString>
  266.    operator+(const TCHAR& a, const CFastString& b)
  267. {
  268.     return CStringSumTCHAR2<CFastString>(a, b);
  269. }
  270.  
  271. inline const CStringSum<CFastString,CStringSumBase>
  272.    operator+(const CFastString& a, const CStringSumBase& b)
  273. {
  274.     return CStringSum<CFastString,CStringSumBase>(a, b);
  275. }
  276. inline const CStringSum<CStringSumBase,CFastString>
  277.    operator+(const CStringSumBase& a, const CFastString& b)
  278. {
  279.     return CStringSum<CStringSumBase,CFastString>(a, b);
  280. }
  281.  
  282. inline const CStringSum<CFastString,CStringSumBase>
  283.    operator+(const CString& a, const CStringSumBase& b)
  284. {
  285.     return static_cast<const CFastString&>(a)+b;
  286. }
  287. inline const CStringSum<CStringSumBase,CFastString>
  288.    operator+(const CStringSumBase& a, const CString& b)
  289. {
  290.     return a+static_cast<const CFastString&>(b);
  291. }
  292.  
  293. inline const CStringSumLPCTSTR1<CStringSumBase>
  294.    operator+(const CStringSumBase& a, const LPCTSTR b)
  295. {
  296.     return CStringSumLPCTSTR1<CStringSumBase>(a, b);
  297. }
  298. inline const CStringSumLPCTSTR2<CStringSumBase>
  299.    operator+(const LPCTSTR a, const CStringSumBase& b)
  300. {
  301.     return CStringSumLPCTSTR2<CStringSumBase>(a, b);
  302. }
  303.  
  304. inline const CStringSumTCHAR1<CStringSumBase>
  305.    operator+(const CStringSumBase& a, const TCHAR& b)
  306. {
  307.     return CStringSumTCHAR1<CStringSumBase>(a, b);
  308. }
  309. inline const CStringSumTCHAR2<CStringSumBase>
  310.    operator+(const TCHAR& a, const CStringSumBase& b)
  311. {
  312.     return CStringSumTCHAR2<CStringSumBase>(a, b);
  313. }
  314.  
  315. inline const CStringSum<CStringSumBase,CStringSumBase>
  316.    operator+(const CStringSumBase& a, const CStringSumBase& b)
  317. {
  318.     return CStringSum<CStringSumBase,CStringSumBase>(a, b);
  319. }
  320.  
  321. inline const CFastString&
  322.    CFastString::operator+=(const CStringSumBase& sum)
  323. {
  324.     return *this = *this+sum;
  325. }
  326.  
  327. #endif  // _FASTSTR_H
  328.